package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.HOSTAUDIT;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.IMTOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USERID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USER_NAME;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.GreetAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.CallContent;
import com.luoxiang.socialandroid.bean.GreetBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.dialog.EditDialog;
import com.luoxiang.socialandroid.dialog.TipDialog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.AlertDialogGreet;
import com.luoxiang.socialandroid.utils.AlertDialogUtils;
import com.luoxiang.socialandroid.utils.LoginUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;
import com.lxj.xpopup.XPopup;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.kit.corekit.im.login.LoginCallback;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tcl.tclzjpro.common.manager.CacheDataManager;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;

public class GreetActivity extends BaseActivity {
    private ImageView icon_back;
    private RecyclerView rc_greet;
    private String token;
    private TextView tx_add;
    private RelativeLayout r_add;
    private GreetAdapter adapter;
    private List<CallContent> list;
    private SmartRefreshLayout smartRefreshLayout;
    private int size;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        r_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditDialog tipDialog = new EditDialog(GreetActivity.this);
                tipDialog.setTitle(getString(R.string.mi_xml_tianjiazhaohuyu));
                tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        tipDialog.dismiss();
                        return null;
                    }
                });
                tipDialog.setOnConfirmClickListener(new Function1<String, Unit>() {
                    @Override
                    public Unit invoke(String s) {
                        addCallContent(token, EXTRA, s);

                        smartRefreshLayout.autoRefresh();
                        tipDialog.dismiss();
                        return null;
                    }
                });
                new XPopup.Builder(GreetActivity.this)
                        .asCustom(tipDialog)
                        .show();
            }
        });
    }

    private void addCallContent(String token, String extra, String text) {
        NetUtils2.getInstance().getApiService().addCallContent(token, extra, text)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            if (bean.getCode() == 0) {
                                ToastUtils.showShort(R.string.mi_zhaohuyuyitijiaoshenhe);
                            } else {
                                ToastUtils.showShort(bean.getMsg());
                            }
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_greet);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        showLoading();
        icon_back = findViewById(R.id.icon_back);
        rc_greet = findViewById(R.id.rc_greet);
        tx_add = findViewById(R.id.tx_add);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        r_add = findViewById(R.id.r_add);
        smartRefreshLayout = findViewById(R.id.smartRefresh);
        selectFirstHostCallCentent(token, EXTRA);
        getHostCallContents(token, EXTRA);
        getRequest();
        smartRefreshLayout.setEnableRefresh(true);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getRequest();
//                getbanner(token, EXTRA);
                getHostCallContents(token, EXTRA);
            }
        });
    }


    /**
     * 查询招呼语列表
     *
     * @param token
     * @param extra
     */
    private void getHostCallContents(String token, String extra) {
        NetUtils2.getInstance().getApiService().getHostCallContents(token, extra, 0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GreetActivity.this);
                            rc_greet.setLayoutManager(linearLayoutManager);
                            adapter = new GreetAdapter(bean.getData().getCallContents());
                            rc_greet.setAdapter(adapter);
                            size = bean.getData().getCallContents().size();
                            tx_add.setText(getString(R.string.mi_xinzengzhaohuyu) + size + "/10)");
                            adapter.setOnAddClickListener(new GreetAdapter.OnAddClickListener() {
                                @Override
                                public void onItemClicks(int position, int id) {
                                    getRequest();
                                    topCallContent(position, id);
                                }
                            });
                            adapter.setOnAddClickListenerdel(new GreetAdapter.OnAddClickListenerdel() {
                                @Override
                                public void onItemClicksdel(int position, int id) {
                                    TipDialog tipDialog = new TipDialog(GreetActivity.this);
                                    tipDialog.setTitle(getString(R.string.mi_caozuotishi));
                                    tipDialog.setCancel(getString(R.string.mi_quxiao));
                                    tipDialog.setContent(getString(R.string.mi_quedingshanchuzhaohuyu));
                                    tipDialog.setConfirm(getString(R.string.mi_queding));
                                    tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                                        @Override
                                        public Unit invoke() {
                                            tipDialog.dismiss();
                                            return null;
                                        }
                                    });
                                    tipDialog.setOnConfirmClickListener(new Function0<Unit>() {
                                        @Override
                                        public Unit invoke() {
                                            getRequest();
                                            delCallContent(position, id);
                                            tipDialog.dismiss();
                                            return null;


                                        }
                                    });
                                    new XPopup.Builder(GreetActivity.this)
                                            .asCustom(tipDialog)
                                            .show();


                                }
                            });
                            adapter.setOnAddClickListenered(new GreetAdapter.OnAddClickListenered() {
                                @Override
                                public void onItemClicksed(String content, int id, String size) {
                                    EditDialog tipDialog = new EditDialog(GreetActivity.this);
                                    tipDialog.setTitle(getString(R.string.mi_bianjizhaohuyu));

                                    tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                                        @Override
                                        public Unit invoke() {
                                            tipDialog.dismiss();
                                            return null;
                                        }
                                    });
                                    tipDialog.setOnConfirmClickListener(new Function1<String, Unit>() {
                                        @Override
                                        public Unit invoke(String s) {
                                            getRequest();
                                            editCallContent(token, EXTRA, s, id);
                                            tipDialog.dismiss();
                                            return null;
                                        }
                                    });
                                    new XPopup.Builder(GreetActivity.this)
                                            .asCustom(tipDialog)
                                            .show();
                                }
                            });
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                        smartRefreshLayout.finishRefresh();
                    }
                });
    }

    private void editCallContent(String token, String extra, String text, int id) {
        NetUtils2.getInstance().getApiService().editCallContent(token, EXTRA, text, id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            if (bean.getCode() == 0) {
                                ToastUtils.showShort(R.string.mi_bianjichenggong);
                            } else {
                                ToastUtils.showShort(bean.getMsg());
                            }
                            getHostCallContents(token, EXTRA);
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void delCallContent(int position, int id) {
        NetUtils2.getInstance().getApiService().delCallContent(token, EXTRA, String.valueOf(id))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            if (bean.getCode() == 0) {
                                ToastUtils.showShort(R.string.mi_delete_success);
                            } else {
                                ToastUtils.showShort(bean.getMsg());

                            }
                            getHostCallContents(token, EXTRA);
                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void topCallContent(int position, int id) {
        NetUtils2.getInstance().getApiService().topCallContent(token, EXTRA, id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            if (bean.getCode() == 0) {
                                ToastUtils.showShort(R.string.mi_zhidingchenggong);
                                getHostCallContents(token, EXTRA);
                            } else {
                                ToastUtils.showShort(bean.getMsg());
                            }

                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void selectFirstHostCallCentent(String token, String extra) {
        NetUtils2.getInstance().getApiService().selectFirstHostCallCentent(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            LogUtils.i("decryptStr00", decryptStr);
                            Gson gson = new Gson();
                            GreetBean bean = gson.fromJson(decryptStr, GreetBean.class);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GreetActivity.this);
                            rc_greet.setLayoutManager(linearLayoutManager);
                            adapter = new GreetAdapter(bean.getData().getCallContents());
                            rc_greet.setAdapter(adapter);

                        } catch (Exception e) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
